﻿using Snake.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Snake
{
    public partial class Frm_Barrier : Form
    {
        string username;

        public Frm_Barrier()
        {
            InitializeComponent();
        }

        public Frm_Barrier(string username)
        {
            InitializeComponent();
            lab_title.Text = "Snaker:" + username;
            this.username = username;
            var barlist = HttpRequest.GetBarrierList();
            InitList(barlist);
            lbx_barrier.ItemHeight = 70;
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btn_min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void InitList(List<string> vs)
        {
            foreach(var p in vs)
            {
                lbx_barrier.Items.Add(p);
            }
        }

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void Frm_Barrier_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        private void lbx_barrier_SelectedIndexChanged(object sender, EventArgs e)
        {
            var id = lbx_barrier.SelectedIndex;
            var resultb = HttpRequest.GetBarrierInfo(id + 1);
            var vs = resultb.Split('%');
            tbx_author.Text = vs[0];
            tbx_dif.Text = vs[1];
            tbx_info.Text = vs[2];
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            var id = lbx_barrier.SelectedIndex + 1;
            Hide();
            new Frm_Game(id, username).ShowDialog();
            Show();
        }

        private void btn_ranking_Click(object sender, EventArgs e)
        {
            var id = lbx_barrier.SelectedIndex + 1;
            new Frm_Ranking(id).ShowDialog();
        }
        protected override void OnShown(EventArgs e)
        {
            Graphics g = CreateGraphics();
            g.DrawRectangle(new Pen(Brushes.White, 3f), 0, 0, Width - 3, Height - 3);
        }
    }
}
